package pl.gigiel.behaviour.environment;

import java.util.ArrayList;
import java.util.List;

import pl.gigiel.behaviour.IBoid;
import pl.gigiel.behaviour.IEnvironment;
import pl.gigiel.behaviour.exception.BehaviourException;
import pl.gigiel.behaviour.util.BorderGuard;

import com.jme.math.Vector3f;

public class SimpleEnvironment implements IEnvironment {
	private BorderGuard borderGuard;
	private List<IBoid> boids;

	public SimpleEnvironment() {
		borderGuard = new BorderGuard();
		boids = new ArrayList<IBoid>();
	}

	@Override
	public void init() throws BehaviourException {
		// nothing
	}

	@Override
	public List<IBoid> getNeighbours(IBoid boid) throws BehaviourException {
		List<IBoid> neighbours = new ArrayList<IBoid>();
		for (IBoid item : boids) {
			if (item != boid) {
				neighbours.add(item);
			}
		}
		return neighbours;
	}

	@Override
	public void add(IBoid boid) {
		boids.add(boid);
	}

	@Override
	public void setBorder(Vector3f cornerMin, Vector3f cornerMax) {
		borderGuard.set(cornerMin, cornerMax);
	}

	@Override
	public Vector3f calculatedPosition(IBoid boid, float elapsed, Vector3f velocity) throws BehaviourException {
		Vector3f position = boid.getFeatures().getPosition();
		position.addLocal(velocity.mult(elapsed));
		borderGuard.correctedPositionLocal(position);
		return position;
	}
}
